package edu.dlust.trading_platform.DataBase;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class UserSQLite extends SQLiteOpenHelper{

        public UserSQLite(Context context) {
            super(context, "user.db", null, 1);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE user(_id INTEGER PRIMARY KEY AUTOINCREMENT, uid VARCHAR(20), psswd VARCHAR(20))");
        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

        }
    // 添加用户
    public void addUser(String uid, String psswd) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("uid", uid);
        values.put("psswd", psswd);
        long result = db.insert("user", null, values);
        db.close();
    }

    // 删除用户
    public void deleteUser() {
        SQLiteDatabase db = this.getWritableDatabase();
        int result = db.delete("user", null, null);
        db.close();
    }


    // 查询用户信息
    public Cursor getUser() {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query("user", null, null, null, null, null, null);
        return cursor;
    }
}
